﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformCommon;

namespace L9SubSet
{
	public partial class Form_Password : Form
	{
		private string password = "";

		private string section = "Password";			// 多语言文件配置节名称

		private string tip = "提示";						// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_Password(string pwd)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.password = pwd;

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "password.Text");
			passLabel.Text = LanguageOper.ReadValue(section, "passLabel.Text");
			okButton.Text = LanguageOper.ReadValue(section, "okButton.Text");
		}

		// 用户已输入密码
		public bool hasInputPassword()
		{
			if (passTextBox.Text.Trim().Equals(""))
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		// 确定
		private void okButton_Click(object sender, EventArgs e)
		{
			if (password == null || password.Equals(""))
			{
				this.DialogResult = System.Windows.Forms.DialogResult.No;
			}

			string[] pwds = password.ToLower().Split(',');
			bool correct = false;
			foreach (string p in pwds)
			{
				if (p.Equals(passTextBox.Text.ToString().ToLower()))
				{
					correct = true;
					break;
				}
			}

			if (correct == true)
			{
				this.DialogResult = System.Windows.Forms.DialogResult.Yes;
			}
			else
			{
				this.DialogResult = System.Windows.Forms.DialogResult.No;
			}
		}

		// 响应 Esc - 关闭窗口
		protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
		{
			int WM_KEYDOWN = 256;
			int WM_SYSKEYDOWN = 260;

			if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
			{
				switch (keyData)
				{
					case Keys.Escape:
						this.Close();
						break;
					case Keys.Enter:
						okButton_Click(null, null);
						break;
				}
			}

			return false;
		}
	}
}
